package de.clapp.musicbox;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import de.clapp.Settings;
import de.clapp.data.MusicItem;
import de.clapp.musicbox.listAdapter.TitleListAdapter;
import de.clapp.musicbox.loader.MusicVoteLoader;

public class MusicVoteModel implements LoaderCallbacks<ArrayList<MusicItem>>
{
    private List<TitleListAdapter> observerList = new ArrayList<TitleListAdapter>();
    
    public static int votesCounter;
    
    /**
     * Eine Activity zum Starten von Loadern
     */
    private Activity activity;
    
    private boolean listen;
    
    private Fragment fragment;
    
    /*
     * Singleton Pattern
     */
    private static MusicVoteModel instance = new MusicVoteModel();

    /*
     * Singleton Pattern
     */
    public static MusicVoteModel getI()
    {
        return MusicVoteModel.instance;
    }

    /*
     * Singleton Pattern
     */
    private MusicVoteModel()
    {

    }
    
    public void setFragment(Fragment fragment)
    {
        this.fragment = fragment;
    }
    
    public void startListeningForVotes()
    {
        this.listen = true;
        this.fragment.getLoaderManager().initLoader(MusicVoteLoader.MUSICVOTELOADER_ID, null, this).forceLoad();
    }
    
    public void stopListeningForVotes()
    {
        this.listen = false;
    }
    
    public void addObserver(TitleListAdapter observer)
    {
        this.observerList.add(observer);
    }
    
    public void fireVotesChanged()
    {
        for(TitleListAdapter observer : this.observerList)
        {
            observer.votesChanged();
        }
    }
   
    public void countVotes()
    {
        ArrayList<MusicItem> items = MusicModel.getI().getSongList();
        if(items == null)
        {
            return;
        }
        for(MusicItem item: items)
        {
            votesCounter += item.getVotes();
        }
        this.fireVotesChanged();
    }
    
    private void updateVotes(ArrayList<MusicItem> result)
    {
        if(result == null)
        {
            return;
        }
        
        votesCounter = 0;
        MusicModel model = MusicModel.getI();
        for(MusicItem item : result)
        {
            MusicItem itemToUpdate = model.getSong(item.getInterpret(), item.getAlbum(), item.getTitel());
            itemToUpdate.setVotes(item.getVotes());
            votesCounter += item.getVotes();
        }
        
        this.fireVotesChanged();
    }
    
    public void setParentActivity(Activity activity)
    {
        this.activity = activity;
    }
    
    @Override
    public void onLoadFinished(android.support.v4.content.Loader<ArrayList<MusicItem>> arg0, ArrayList<MusicItem> result)
    {
        this.updateVotes(result);
        if(this.listen)
        {
            this.fragment.getLoaderManager().initLoader(Settings.LOADERNUMBER++, null, this).forceLoad();
        }
    }

    @Override
    public void onLoaderReset(android.support.v4.content.Loader<ArrayList<MusicItem>> arg0)
    {
        
    }

    @Override
    public android.support.v4.content.Loader<ArrayList<MusicItem>> onCreateLoader(int arg0, Bundle arg1)
    {
        return new MusicVoteLoader(this.activity);
    }
}
